/*
 * Copyright (C) 2013～2023 上海颐凡软件科技有限公司
 * Yfann Software Technology (Shanghai) Co.,LTD
 * All Rights Reserved.
 * 公司网址: www.yfann.com
 * 365IT教育网，成就您优秀架构师之梦！
 * 平台网址: www.365itedu.com
 */
package com.yfann.web.base.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.yfann.sword.web.fwk.bean.UIOBean;
import com.yfann.sword.web.fwk.consts.FrameworkConst;
import com.yfann.sword.web.fwk.filter.AuthenticationController;
import com.yfann.sword.web.fwk.util.PropertyUtil;
import com.yfann.sword.web.fwk.util.RequestUtil;
import com.yfann.web.base.sys.consts.SYSConst;

/**
 * 判断用户是否已经登陆系统实现。
 * 
 * <pre>
 * 修改履历
 * -----------------------------------------------------------------------------
 *         VERSION       DATE            BY       CHANGE/COMMENT
 * -----------------------------------------------------------------------------
 *          1.0         2013/10/11      颜廷吉            create
 * -----------------------------------------------------------------------------
 * </pre>
 */
public class AuthenticationControllerImpl implements AuthenticationController {

    /**
     * 登录中的情况下返回true；还没有登录的情况下返回false。
     * @param pathInfo String
     * @param req ServletRequest
     * @return boolean
     */
    @Override
    public boolean isAuthenticated(String pathInfo, ServletRequest req) {
        HttpServletRequest httpRequest = (HttpServletRequest) req;
        HttpSession session = httpRequest.getSession();
        UIOBean uio = (UIOBean) session.getAttribute(FrameworkConst.USER_INFO_OBJECT_KEY);
        // 会话里面，如果有UserValueObject的信息，说明登录成功。
        if (uio != null) {
            return true;
        }
        return false;
    }

    /**
     * 需要登录中检查的情况下返回true；不需要的情况下返回false。
     * @param req ServletRequest
     * @return boolean
     */
    @Override
    public boolean isCheckRequired(ServletRequest req) {

        List<String> noCheckList = null;

        String pathInfo = RequestUtil.getActionPathInfo(req);
        noCheckList = new ArrayList<String>();
        for (int i = 1;; i++) {
            String path = PropertyUtil.getProperty(SYSConst.AUTHENTICATED_NOCHECK_PATH_PREFIX + i);
            if (path == null) {
                break;
            }
            noCheckList.add(path);
        }

        for (String path : noCheckList) {
            if (pathInfo.equals(path)) {
                return false;
            }
        }
        return true;
    }
}
